"""
Problem 59: https://projecteuler.net/problem=59

Each character on a computer is assigned a unique code and the preferred standard is
ASCII (American Standard Code for Information Interchange).
For example, uppercase A = 65, asterisk (*) = 42, and lowercase k = 107.

A modern encryption method is to take a text file, convert the bytes to ASCII, then
XOR each byte with a given value, taken from a secret key. The advantage with the
XOR function is that using the same encryption key on the cipher text, restores
the plain text; for example, 65 XOR 42 = 107, then 107 XOR 42 = 65.

For unbreakable encryption, the key is the same length as the plain text message, and
the key is made up of random bytes. The user would keep the encrypted message and the
encryption key in different locations, and without both "halves", it is impossible to
decrypt the message.

Unfortunately, this method is impractical for most users, so the modified method is
to use a password as a key. If the password is shorter than the message, which is
likely, the key is repeated cyclically throughout the message. The balance for this
method is using a sufficiently long password key for security, but short enough to
be memorable.

Your task has been made easy, as the encryption key consists of three lower case
characters. Using p059_cipher.txt (right click and 'Save Link/Target As...'), a
file containing the encrypted ASCII codes, and the knowledge that the plain text
must contain common English words, decrypt the message and find the sum of the ASCII
values in the original text.
"""

# _*_ conding:UTF-8 _*_
'''
@author = Kuperain
@email = kuperain@aliyun.com
@IDE = VSCODE Python3.8.3
@creat_time = 2022/5/19
'''




from pyparsing import itertools
def encrypt(msg: str, key: str) -> str:
    '''
    >>> print(encrypt('AAAAA','***'))
    kkkkk
    '''
    msglens = len(msg)
    keylens = len(key)

    cipher = ''.join(
        [chr(
            ord(msg[i]) ^ ord(key[i % keylens]))
         for i in range(msglens)])

    return cipher


def decrypt(cipher: str, key: str) -> str:
    '''
    >>> print(decrypt(encrypt('AAAAA','***'),'***'))
    AAAAA
    '''
    return encrypt(cipher, key)


def solution(cipherfile: str = 'p059_cipher.txt') -> int:
    '''

    '''

    with open(cipherfile, 'r') as cf:
        cipher = cf.read()
    cipher = eval('['+cipher.strip() + ']')
    cipher = ''.join(map(chr, cipher))
    # print(cipher)

    # key consists of three lower case characters
    characters = list(map(chr, (range(ord('a'), ord('z')+1))))
    keyspace = itertools.permutations(characters, 3)

    # cryptanalysis
    for key in keyspace:
        key = ''.join(key)
        msg = decrypt(cipher, key)
        if ' is ' in msg and (
           ' are ' in msg) and (
                ' the ' in msg) and (
                ' to ' in msg):
            print(msg, key)

    return sum(map(ord, msg))


if __name__ == "__main__":
    import doctest
    doctest.testmod(verbose=False)

    print(solution())
    # 136672

    '''
    An extract taken from the introduction of one of 
    Euler's most celebrated papers, "De summis serierum 
    reciprocarum" [On the sums of series of reciprocals]: 
    I have recently found, quite unexpectedly, an elegant 
    expression for the entire sum of this series 1 + 1/4 +
    1/9 + 1/16 + etc., which depends on the quadrature 
    of the circle, so that if the true sum of this series 
    is obtained, from it at once the quadrature of the 
    circle follows. Namely, I have found that the sum of 
    this series is a sixth part of the square of the 
    perimeter of the circle whose diameter is 1; or by 
    putting the sum of this series equal to s, it has 
    the ratio sqrt(6) multiplied by s to 1 of the 
    perimeter to the diameter. I will soon show that the 
    sum of this series to be approximately 
    1.644934066842264364; and from multiplying this number
    by six, and then taking the square root, the number 
    3.141592653589793238 is indeed produced, which 
    expresses the perimeter of a circle whose diameter 
    is 1. Following again the same steps by which I had 
    arrived at this sum, I have discovered that the sum 
    of the series 1 + 1/16 + 1/81 + 1/256 + 1/625 + etc. 
    also depends on the quadrature of the circle. Namely, 
    the sum of this multiplied by 90 gives the biquadrate 
    (fourth power) of the circumference of the perimeter 
    of a circle whose diameter is 1. And by similar reasoning 
    I have likewise been able to determine the sums of the 
    subsequent series in which the exponents are even numbers. 
      
    key = exp
    '''
